﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilPublic.ServerLoger;
using StackExchange.Redis;
using System.Collections.Generic;

namespace UtilPublic.Redis
{
    /// <summary>
    /// 排行榜公共数据
    /// </summary>
    public class BaseTopDataRedis
    {
        /// <summary>
        /// 排行
        /// </summary>
        public int Rank;
        /// <summary>
        /// 唯一ID
        /// </summary>
        public string Puid;
        /// <summary>
        /// 得分
        /// </summary>
        public double Score;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="r"></param>
        /// <param name="p"></param>
        /// <param name="s"></param>
        public BaseTopDataRedis(int r, string p, double s)
        {
            Rank = r;
            Puid = p;
            Score = s;
        }
    }

    /// <summary>
    /// SortedSet存储
    /// </summary>
    public partial class RedisLink
    {
        /// <summary>
        /// 加入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="score"></param>
        public void StSetAdd<T>(string key, T value, long score)
        {
            try
            {
                RedisDb.SortedSetAdd(key, $"{value}", score);
            }
            catch
            {
                Zloger.Fatal($"StSetAdd Error {key} {value} {score}");
            }
        }
        /// <summary>
        /// 增量
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="score"></param>
        public void StSetInc<T>(string key, T value, long score)
        {
            try
            {
                RedisDb.SortedSetIncrement(key, $"{value}", score);
            }
            catch
            {
                Zloger.Fatal($"StSetInc Error {key} {value} {score}");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void StSetRemove<T>(string key, T value)
        {
            try
            {
                RedisDb.SortedSetRemove(key, $"{value}");
            }
            catch
            {
                Zloger.Fatal($"StSetRemove Error {key} {value}");
            }
        }
        /// <summary>
        /// 获取排行
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="Descending"></param>
        /// <returns></returns>
        public long StSetRank<T>(string key, T value, bool Descending = false)
        {
            Order order = Order.Ascending;
            if (Descending)
            {
                order = Order.Descending;
            }
            var result = RedisDb.SortedSetRank(key, $"{value}", order);
            if (result == null)
            {
                return -1;
            }
            else
            {
                return (long)result + 1;
            }
        }
        /// <summary>
        /// 获取分数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public long StSetScore<T>(string key, T value)
        {
            var result = RedisDb.SortedSetScore(key, $"{value}");
            if (result == null)
            {
                return 0;
            }
            else
            {
                return (long)result;
            }
        }
        /// <summary>
        /// 获取排行和分数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="rank"></param>
        /// <param name="score"></param>
        /// <returns></returns>
        public void StSetRankScore<T>(string key, T value, out long rank, out long score)
        {
            rank = StSetRank(key, value);
            score = StSetScore(key, value);
        }
        /// <summary>
        /// 获取长度
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long StSetGetLength(string key)
        {
            try
            {
                return RedisDb.SortedSetLength(key);
            }
            catch
            {
                Zloger.Fatal($"MssetGetCount Error {key}");
                return 0;
            }
        }
        /// <summary>
        /// 获取列表 根据排名
        /// </summary>
        /// <param name="key"></param>
        /// <param name="startRank"></param>
        /// <param name="stopRank"></param>
        /// <param name="Descending"></param>
        /// <returns></returns>
        public List<BaseTopDataRedis> StSetRangeByRank(string key, int startRank, int stopRank, bool Descending = false)
        {
            var result = new List<BaseTopDataRedis>();
            try
            {
                Order order = Order.Ascending;
                if (Descending)
                {
                    order = Order.Descending;
                }
                var rr = RedisDb.SortedSetRangeByRankWithScores(key, startRank, stopRank, order);
                int rank = 1;
                foreach (var nn in rr)
                {
                    result.Add(new BaseTopDataRedis(rank, nn.Element, (long)nn.Score));
                    rank++;
                }
            }
            catch
            {
                Zloger.Fatal($"StSetRangeByRank Error {key} {startRank} {stopRank}");
            }
            return result;
        }
        /// <summary>
        /// 获取列表 根据页码
        /// </summary>
        /// <param name="key"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <param name="Descending"></param>
        /// <returns></returns>
        public List<string> StSetRangeByPage(string key, int pageIndex = 0, int pageCount = 100, bool Descending = false)
        {
            var result = new List<string>();
            int startRank = pageCount * pageIndex;
            int stopRank = (pageIndex + 1) * pageCount - 1;
            try
            {
                Order order = Order.Ascending;
                if (Descending)
                {
                    order = Order.Descending;
                }
                var rr = RedisDb.SortedSetRangeByRankWithScores(key, startRank, stopRank, order);
                int rank = 1;
                foreach (var nn in rr)
                {
                    result.Add(nn.Element);
                    rank++;
                }
            }
            catch
            {
                Zloger.Fatal($"StSetRangeByPage Error {key} {pageIndex} {pageCount}");
            }
            return result;
        }
        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="key"></param>
        public void StSetRemoveAll(string key)
        {
            try
            {
                int allrank = (int)StSetGetLength(key);
                RedisDb.SortedSetRemoveRangeByRank(key, 0, allrank);
            }
            catch
            {
                Zloger.Fatal($"StSetRemoveAll Error {key}");
            }
        }
        /// <summary>
        /// 获取排行榜
        /// </summary>
        /// <param name="key"></param>
        /// <param name="Descending">降序</param>
        /// <returns></returns>
        public List<BaseTopDataRedis> StSetRangeByRankAll(string key, bool Descending = false)
        {
            try
            {
                int allrank = (int)StSetGetLength(key);
                return StSetRangeByRank(key, 0, allrank, Descending);
            }
            catch
            {
                Zloger.Fatal($"StSetRangeByRankAll Error {key}");
            }
            return new List<BaseTopDataRedis>();
        }
        
    }
}